﻿using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;

namespace SHomeWorkshop.LunarPresentation.Adorners
{
    /// <summary>
    /// 主窗口的MainTabControl使用的装饰器。
    /// </summary>
    public class TabControlAdorner : Adorner
    {
        /// <summary>
        /// 主窗口的MainTabControl使用的装饰器。
        /// </summary>
        /// <param name="adornedElement">应传入主窗口的“MainTabControl”主子元素。</param>
        public TabControlAdorner(UIElement adornedElement)
            : base(adornedElement)
        {
            //终于解决了选取装饰器中线条宽度为2而不是1像素的问题！！！
            this.SnapsToDevicePixels = true;
            this.SetValue(RenderOptions.EdgeModeProperty, EdgeMode.Aliased);

            this.MouseEnter += new System.Windows.Input.MouseEventHandler(TabControlAdorner_MouseEnter);
            this.MouseLeave += new System.Windows.Input.MouseEventHandler(TabControlAdorner_MouseLeave);
        }

        void TabControlAdorner_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            InvalidateVisual();
        }

        void TabControlAdorner_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            InvalidateVisual();
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            Rect rect = new Rect(this.AdornedElement.RenderSize);

            Brush renderBrush;
            if (IsMouseOver)
            {
                renderBrush = Brushes.WhiteSmoke;
            }
            else
            {
                renderBrush = Brushes.Transparent;
            }
            Pen renderPen = new Pen(Brushes.Blue, 1);

            drawingContext.DrawRectangle(renderBrush, renderPen,
                new Rect(rect.Right - 16, rect.Top + 4, 16, 16));
        }
    }
}